package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;

import java.awt.Font;
import java.util.ArrayList;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import bus.SubjectBus;
import dto.SubjectDto;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class JDialogStudentFindSubjectId extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField txtSubjectName;
	private JTextField txtSubjectId;
	private DefaultTableModel model = new DefaultTableModel();
	private JTable tblSubject;

	public String subjectId = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JDialogStudentFindSubjectId dialog = new JDialogStudentFindSubjectId();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public JDialogStudentFindSubjectId() {
		setModalityType(ModalityType.TOOLKIT_MODAL);
		setModal(true);
		setLocationByPlatform(true);
		setTitle("Student - Find subject id");
		setBounds(100, 100, 437, 381);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		JLabel label = new JLabel("Enter subject name:");
		label.setFont(new Font("Tahoma", Font.BOLD, 12));
		label.setBounds(63, 35, 227, 14);
		contentPanel.add(label);

		JLabel label_1 = new JLabel("Subject name:");
		label_1.setBounds(63, 60, 100, 14);
		contentPanel.add(label_1);

		txtSubjectName = new JTextField();
		txtSubjectName.setColumns(10);
		txtSubjectName.setBounds(163, 57, 167, 20);
		txtSubjectName.getDocument().addDocumentListener(
				new DocumentListener() {
					public void changedUpdate(DocumentEvent e) {
						// text was changed

					}

					public void removeUpdate(DocumentEvent e) {
						// text was deleted
						searchSubject();
					}

					public void insertUpdate(DocumentEvent e) {
						// text was inserted

						searchSubject();
					}
				});

		contentPanel.add(txtSubjectName);

		txtSubjectId = new JTextField();
		txtSubjectId.setFont(new Font("Tahoma", Font.BOLD, 12));
		txtSubjectId.setEnabled(false);
		txtSubjectId.setColumns(10);
		txtSubjectId.setBounds(163, 85, 167, 20);
		contentPanel.add(txtSubjectId);

		JLabel label_2 = new JLabel("Subject ID:");
		label_2.setBounds(63, 88, 80, 14);
		contentPanel.add(label_2);

		JLabel label_3 = new JLabel("List subjects:");
		label_3.setFont(new Font("Tahoma", Font.BOLD, 11));
		label_3.setBounds(41, 118, 227, 14);
		contentPanel.add(label_3);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(41, 143, 343, 157);
		contentPanel.add(scrollPane);

		tblSubject = new JTable();
		tblSubject.getSelectionModel().addListSelectionListener(
				new ListSelectionListener() {
					@Override
					public void valueChanged(ListSelectionEvent event) {
						// do some actions here, for example
						// print first column value from selected row
						try {
							subjectId = tblSubject.getValueAt(
									tblSubject.getSelectedRow(), 0).toString();
						} catch (Exception e) {
							// TODO: handle exception
							subjectId = "";
						}
						System.out.println(subjectId);
						txtSubjectId.setText(subjectId);
					}
				});
		model = new DefaultTableModel(new Object[][] {}, new String[] {
				"Subject Id", "Subject Name" }) {
			Class[] columnTypes = new Class[] { String.class, String.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		};
		tblSubject.setModel(model);

		scrollPane.setViewportView(tblSubject);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnOK = new JButton("OK");
				btnOK.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						JDialogStudentFindSubjectId.this.dispose();
					}
				});
				btnOK.setActionCommand("OK");
				buttonPane.add(btnOK);
				getRootPane().setDefaultButton(btnOK);
			}
		}
	}

	private void searchSubject() {
		String subjectName = txtSubjectName.getText();
		if (subjectName.equals("") == false) {

			for (int i = model.getRowCount()-1; i >=0; i--)
				model.removeRow(i);

			ArrayList<SubjectDto> listObject = SubjectBus
					.getSubjectByName(subjectName);
			for (int i = 0; i < listObject.size(); i++) {
				SubjectDto subject = listObject.get(i);

				String[] newTableColumn = { subject.getSubjectId(),
						subject.getSubjectName() };
				model.addRow(newTableColumn);
			}

		}
	}
}
